#coding=utf-8 
from django.shortcuts import render_to_response
from django.http import HttpResponse
from stock.models import *

def share_details(request, c):
    s = Share.objects.get(code=c)
    eds = s.exday_set.order_by('-date')[:80]
    ps = [e.price for e in eds]
    return render_to_response('share_details.html', {'share':s, 'prices':ps})

def test_matplotlib(request):
    
    import matplotlib
    matplotlib.use('Cairo')
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
    from StringIO import StringIO
    
    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    ax.plot([1,2,3])
    ax.set_title('hi mom')
    ax.grid(True)
    ax.set_xlabel('time')
    ax.set_ylabel('volts')
    canvas.draw()
    
    imdata=StringIO()
    fig.savefig(imdata,format='png')
    
    return HttpResponse(imdata.getvalue(), mimetype='image/png')